#include "Vector.h"

Vector::Vector(const fp _x, const fp _y, const fp _z)
{
  x = _x;
  y = _y;
  z = _z;
};  

Vector::Vector(const Vector& b) { this->operator=(b); };

void Vector::Clear() { memset(c, 0, sizeof(c)); };  

fp Vector::Magnitude() const { return sqrt(x*x + y*y + z*z); };  

void Vector::Unitize()
{
  fp magnitude = Magnitude();
  if (magnitude <= 0) return;
  
  this->operator*=(1.0f / magnitude);
};  

Vector Vector::operator-() { return this->operator*(-1.0f); };

Vector Vector::operator*(const fp k) const { return Vector(x*k, y*k, z*k); };

Vector& Vector::operator*=(const fp k)
{
  x*=k;
  y*=k;
  z*=k;

  return *this;
};

Vector Vector::operator/(const fp k) const
{
  if (k <= 0) return Vector();
  return this->operator*(k);
};

Vector& Vector::operator/=(const fp k)
{
  if (k > 0) return this->operator *=(1.0f/k);
  return *this;
};

Vector Vector::operator+(const Vector& b) const { return Vector(x+b.x,y+b.y,z+b.z); };
Vector& Vector::operator+=(const Vector& b)
{
  x+=b.x;
  y+=b.y;
  z+=b.z;

  return *this;
};

Vector Vector::operator-(const Vector& b) const { return Vector(x-b.x, y-b.y, z-b.z); };
Vector& Vector::operator-=(const Vector& b)
{
  x-=b.x;
  y-=b.y;
  z-=b.z;

  return *this;
};

Vector& Vector::operator=(const Vector& b)
{
  memcpy(c, b.c, sizeof(c));
  return *this;
};

bool Vector::operator==(const Vector& b) const
{
  if (memcmp(c, b.c, sizeof(c)) == 0) return true;
  return false;
};
bool Vector::operator!=(const Vector& b) const { return !this->operator==(b); };

fp Vector::operator*(const Vector& b) const { return (x*b.x + y*b.y + z*b.z); };

Vector Vector::operator^(const Vector& b) const
{
  return Vector(  y * b.z - z * b.y,
                -(x * b.z - z * b.x),
                  x * b.y - y * b.x   );
};
